﻿// ShutDownDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ShutDownDlg.h"
#include "CTask.h"

// CShutDownDlg dialog

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CShutDownDlg::CShutDownDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CShutDownDlg::IDD, pParent)
{
	m_sUserName = _T("");
	m_sTaskName = _T("環保署定時休眠排程");
	m_dStartTime = 0;
	m_sProgram = _T("");
	m_sPath = _T("");
	m_sPassword = _T("");
	m_sParameters = _T("");
	m_iFreq = 0;
	m_wattPerHr = 120;
	m_dStartDate = CTime::GetCurrentTime();
	CTask::TaskExisted( m_sTaskName, m_bActivate );
}

CShutDownDlg::~CShutDownDlg()
{
}

void CShutDownDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

	DDX_Text(pDX, IDC_WATT_PER_HR, m_wattPerHr);
	DDX_DateTimeCtrl(pDX, IDC_STARTTIME, m_dStartTime);
	DDX_Check(pDX, IDC_ACTIVATE_TASK, m_bActivate);
}


BEGIN_MESSAGE_MAP(CShutDownDlg, CDialog)
	ON_EN_CHANGE(IDC_WATT_PER_HR, OnEnChangeWattPerHr)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_ACTIVATE_TASK, OnBnClickedActivateTask)
END_MESSAGE_MAP()


// CShutDownDlg message handlers

BOOL CShutDownDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	CDateTimeCtrl* pTimeCtl;
	pTimeCtl = static_cast<CDateTimeCtrl*>( GetDlgItem( IDC_STARTTIME ) );

	SYSTEMTIME stBegin;
	m_dStartDate.GetAsSystemTime ( stBegin );
	CTime SuggestTime( stBegin.wYear, stBegin.wMonth, stBegin.wDay, m_taskHour, m_taskMin, 0 );
	pTimeCtl->SetTime( &SuggestTime );
	pTimeCtl->SetFormat ( _T("tt hh:mm") );

	CSpinButtonCtrl *m_spinCtrl;
	CEdit* m_spinEdCtrl;
	m_spinCtrl = static_cast<CSpinButtonCtrl*>( GetDlgItem( IDC_SPIN1 ) );
	m_spinEdCtrl = static_cast<CEdit*>( GetDlgItem( IDC_WATT_PER_HR ) );
	m_spinCtrl->SetBuddy( m_spinEdCtrl );
	m_spinCtrl->SetRange32( 1, 9999 );

	OnBnClickedActivateTask();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CShutDownDlg::OnEnChangeWattPerHr()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
	UpdateData();

	if ( m_wattPerHr > 9999 ) {
		m_wattPerHr = 9999;
	} else if ( m_wattPerHr < 1 ) {
		m_wattPerHr = 1;
	}

	UpdateData( FALSE );
}

void CShutDownDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	UpdateData();

	if ( m_bActivate == TRUE ) { // set schedule
		CTask task;
		SYSTEMTIME stBegin;
		
		m_sProgram = _T( "%WINDIR%\\PsShutdown.exe" );
		m_sParameters = _T( "-accepteula -h -c -t 60" );
		
		task.SetProgram     ( m_sProgram );
		task.SetParameters  ( m_sParameters );
		task.SetStartingDir ( m_sPath );
		task.SetFrequency   ( CTask::freqDaily );
		task.SetAccountName ( m_sUserName );
		task.SetPassword    ( m_sPassword );
		task.SetComment     ( m_sComment );

		m_dStartTime.GetAsSystemTime ( stBegin );
		m_taskHour = m_dStartTime.GetHour();
		m_taskMin = m_dStartTime.GetMinute();
		
		task.SetStartDateTime ( stBegin );
		task.SaveTask ( m_sTaskName, FALSE );
	} else { // delete schedule
		CTask::DeleteTask ( m_sTaskName );
	}

	OnOK();
}

void CShutDownDlg::OnBnClickedActivateTask()
{
	// TODO: Add your control notification handler code here
	UpdateData();

	if ( m_bActivate == FALSE ) {
		GetDlgItem( IDC_STIME_TEXT )->EnableWindow( FALSE );
		GetDlgItem( IDC_STARTTIME )->EnableWindow( FALSE );
	} else {
		GetDlgItem( IDC_STIME_TEXT )->EnableWindow( TRUE );
		GetDlgItem( IDC_STARTTIME )->EnableWindow( TRUE );
	}

	UpdateData( FALSE );
}
